/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelArbol.java
 *
 * Created on 24/09/2011, 17:01:35
 */
package tsb.arbol;


import java.awt.Toolkit;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author Fede
 */
public class PanelArbol extends javax.swing.JPanel {
    
    protected DefaultMutableTreeNode rootNode;
    protected DefaultTreeModel treeModel;
    private Toolkit toolkit = Toolkit.getDefaultToolkit();
    private String path;

    /** Creates new form PanelArbol */
    public PanelArbol() {
        
        initComponents();
        Arbol.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    }

    
    private javax.swing.tree.DefaultTreeModel getModeloRaiz()
    {
         java.net.URL imgURL1 = TreeIconDemo.class.getResource("imagenes/home-icon6.gif");
         ImageIcon Icono1 = new ImageIcon(imgURL1);
         
         java.net.URL imgURL2 = TreeIconDemo.class.getResource("imagenes/action_print.gif");
         ImageIcon Icono2 = new ImageIcon(imgURL2);
         
         java.net.URL imgURL3 = TreeIconDemo.class.getResource("imagenes/action_save.gif");
         ImageIcon Icono3 = new ImageIcon(imgURL3);
         
         java.net.URL imgURL4 = TreeIconDemo.class.getResource("imagenes/action_save.gif");
         ImageIcon Icono4 = new ImageIcon(imgURL4);
         

       //  Icon Icono2 = new ImageIcon("imagenes/action_print.gif");
        // Icon Icono3 = new ImageIcon("imagenes/action_save.gif");
        // Icon Icono4 = new ImageIcon("imagenes/icon_monitor_pc.gif");
         

         
//            DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
//            renderer.setLeafIcon(Icono4);
//            renderer.setOpenIcon(Icono2);
//            renderer.setClosedIcon(Icono3);
//            renderer.setLeafIcon(Icono1);
//            Arbol.setCellRenderer(renderer);
            
            
        rootNode = new DefaultMutableTreeNode(new IconData(Icono1, "Diagrama"));
        treeModel = new DefaultTreeModel(rootNode);
        
        DefaultMutableTreeNode DispositivosFinalesNode = new DefaultMutableTreeNode(new IconData(Icono3, "Dispositivos Finales"));
        
        treeModel.insertNodeInto(DispositivosFinalesNode, rootNode, rootNode.getChildCount());
        
        DefaultMutableTreeNode DispositivosDeConeccionNode = new DefaultMutableTreeNode("Dispositivos De Conección");
        treeModel.insertNodeInto(DispositivosDeConeccionNode, rootNode, rootNode.getChildCount());
        
        DefaultMutableTreeNode PC = new DefaultMutableTreeNode("PC-1");
        
        treeModel.insertNodeInto(PC, DispositivosFinalesNode, DispositivosFinalesNode.getChildCount());
	
        
        
         

        return  treeModel;
    
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollArbol = new javax.swing.JScrollPane();
        Arbol = new javax.swing.JTree();

        Arbol.setModel(getModeloRaiz());
        scrollArbol.setViewportView(Arbol);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollArbol, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollArbol, javax.swing.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree Arbol;
    private javax.swing.JScrollPane scrollArbol;
    // End of variables declaration//GEN-END:variables


/** Remove all nodes except the root node. */
    public void clear() {
        rootNode.removeAllChildren();
        treeModel.reload();
    }
    
        /** Remove the currently selected node. */
    public void removeCurrentNode() {
        TreePath currentSelection = Arbol.getSelectionPath();
        if (currentSelection != null) {
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)(currentSelection.getLastPathComponent());
            MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
            if (parent != null) {
                treeModel.removeNodeFromParent(currentNode);
                return;
            }
        } 

        // Either there was no selection, or the root was selected.
        toolkit.beep();
    }

    /** Add child to the currently selected node. */
    public DefaultMutableTreeNode addObject(Object child) {
        DefaultMutableTreeNode parentNode = null;
        TreePath parentPath = Arbol.getSelectionPath();

        if (parentPath == null) {
            parentNode = rootNode;
        } else {
            parentNode = (DefaultMutableTreeNode)
                         (parentPath.getLastPathComponent());
        }

        return addObject(parentNode, child, true);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child) {
        return addObject(parent, child, false);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child, 
                                            boolean shouldBeVisible) {
        DefaultMutableTreeNode childNode = 
                new DefaultMutableTreeNode(child);

        if (parent == null) {
            parent = rootNode;
        }
	
	//It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
        treeModel.insertNodeInto(childNode, parent, 
                                 parent.getChildCount());

        //Make sure the user can see the lovely new node.
        if (shouldBeVisible) {
            Arbol.scrollPathToVisible(new TreePath(childNode.getPath()));
        }
        return childNode;
    }

   

}